package com.lsy.weizhi2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.utils.MyMethod;

import java.util.ArrayList;
import java.util.List;

public class Welcome extends MyBaseActivity {
    private ViewPager welcome_vp;
    private ImageView qidong_iv;
    private Button qidong_btn;
    private int[]yindaos={R.mipmap.yindao1, R.mipmap.yindao2,R.mipmap.yindao3,R.mipmap.qidong};
    private boolean isfirst;
    private SharedPreferences sharedPreferences;
    private List<View> list;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        context=Welcome.this;
        qidong_iv= (ImageView) findViewById(R.id.qidong_iv);
        qidong_btn= (Button) findViewById(R.id.qidong_btn);
        welcome_vp= (ViewPager) findViewById(R.id.welcome_vp);
        list=new ArrayList<>();
        sharedPreferences=getSharedPreferences("welcome",0);
        isfirst=sharedPreferences.getBoolean("isfirst",true);
        if(isfirst){
            welcome_vp.setVisibility(View.VISIBLE);
        }else{
            qidong_iv.setVisibility(View.VISIBLE);
            qidong_btn.setVisibility(View.VISIBLE);
        }
        qidong_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               MyMethod.toActivity(context,LoginActivty.class);
               // MyMethod.toActivity(context,HomePage.class);
                finish();//关闭欢迎页
            }
        });
        for (int i = 0; i < 4; i++) {
            ImageView imageView=new ImageView(Welcome.this);
            imageView.setImageResource(yindaos[i]);
            list.add(imageView);
        }
        MyPagerAdapter adapter=new MyPagerAdapter();
        welcome_vp.setAdapter(adapter);
        welcome_vp.setCurrentItem(0);
    }
    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView=new ImageView(context);
            imageView.setImageResource(yindaos[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(imageView);
            if(position==3){
                qidong_btn.setVisibility(View.VISIBLE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putBoolean("isfirst",false);
                editor.commit();
            }
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        SharedPreferences sharedPreferences=context.getSharedPreferences(MyInfo.User_info, Activity.MODE_PRIVATE);
        MyInfo.Token =sharedPreferences.getString("token","");
        MyInfo.Name=sharedPreferences.getString("name","");
        MyInfo.Phone=sharedPreferences.getString("phone","");
        MyInfo.User_Id=sharedPreferences.getInt("user_id",0);
        MyInfo.Avatar=sharedPreferences.getInt("avatar",1);
        if(MyInfo.Token!=null&&MyInfo.Token.length()!=0){
            MyMethod.toActivity(context, HomePage.class);
            finish();//关闭欢迎页
        }
    }


}
